<template>
	<view>
		<button @click="addToCart">加入购物车</button>
		<navigator url="../cart/cart" open-type="switchTab">去购物车</navigator>
		详情页面
		<view>{{baseInfo.productName}}</view>
		<view>{{baseInfo.price/100}}</view>
		<view v-if="baseInfo.imageList" class="imageList">
			<image
			 :src="item"
			 :key="item"
			 v-for="item in baseInfo.imageList" mode="widthFix"></image>
		</view>
	</view>
</template>

<script setup>  
	// 导入ref
	import {ref} from 'vue'
    //在setup api中一些生命周期需要导入
	import {onLoad} from '@dcloudio/uni-app'
	// 导入获取当前页面的接口
	import {getBaseInfo,addCart} from '@/api/index.js'
	
	const baseInfo = ref({})
	onLoad(option=>{
		 getBaseInfo({skuId:option.skuId})
		 .then(res=>{
			 baseInfo.value = res.data;
		 })
	})
	// 添加到购物车
	function addToCart(){
		// 执行接口
		addCart({
		  "buyNumber": 1,
		  // 设置参数skuid
		  "skuId": baseInfo.value.skuPicker[0].skuId,
		  "is_tc": false,
		  "tyingItems": []
		})
		.then(res=>{
			if(res.data){
				// 成功弹出提示
				uni.showToast({
					title:"加入购物车成功"
				})
			}
		})
		
	}
	 
</script>

<style>
.imageList image{
	width: 100%;
}
</style>
